﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

/// <summary>
/// ReqHelper 的摘要说明
/// </summary>
public class ReqHelper
{

    public static T Get<T>(string paramName)
    {
        string value = HttpContext.Current.Request.Form[paramName];
        if (value == null)
            value = HttpContext.Current.Request[paramName];

        Type type = typeof(T);
        object result;
        try
        {
            result = Convert.ChangeType(value, type);
        }
        catch
        {
            result = default(T);
        }
        if ((T)result == null)
        {
            HttpContext.Current.Response.ContentType = "text/plain";
            Object obj = new { error = paramName + "参数不可为空" };
            HttpContext.Current.Response.Write(Newtonsoft.Json.JsonConvert.SerializeObject(obj));
            HttpContext.Current.Response.End();
        }
        return (T)result;
    }
    /// <summary>
    /// 检查日期
    /// </summary>
    /// <param name="dateTime"></param>
    /// <returns></returns>
    public static DateTime checkDate(string dateTime)
    {
        DateTime time;
        if (DateTime.TryParse(dateTime.ToString(), out time))
        {
            if (dateTime == "0001/1/1")
            {
                HttpContext.Current.Response.ContentType = "text/plain";
                Object obj = new { error = "日期格式不正确" };
                HttpContext.Current.Response.Write(Newtonsoft.Json.JsonConvert.SerializeObject(obj));
                HttpContext.Current.Response.End();
            }
        }
        else
        {
            HttpContext.Current.Response.ContentType = "text/plain";
            Object obj = new { error = "日期格式不正确" };
            HttpContext.Current.Response.Write(Newtonsoft.Json.JsonConvert.SerializeObject(obj));
            HttpContext.Current.Response.End();
        }
        return time;
    }
}